package com.meixiang.activity.account.manager;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbToastUtil;
import com.meixiang.util.DesUtil;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 短信修改登录密码
 *
 * @author zys
 */
public class ChangeLoginPasswordOneActivity extends BaseActivity {
    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.cet_input_new_password)
    ClearEditText cetInputNewPassword;
    @Bind(R.id.cet_input_affirm_new_pwd)
    ClearEditText cetInputAffirmNewPwd;
    @Bind(R.id.tv_update_acknowledge)
    CheckEditTextEmptyButton tvUpdateAcknowledge;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_login_pwd_one);
        ButterKnife.bind(this);
        mActivity=this;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        title = (TitleView) this.findViewById(R.id.title);
        title.setTitle(R.string.change_password);
        title.setLeftImageButton(R.mipmap.back);
        title.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    @Override
    protected void initView() {
        tvUpdateAcknowledge.setEditText(new EditText[]{cetInputNewPassword,cetInputAffirmNewPwd});
        cetInputNewPassword.setOnClickListener(this);
        cetInputAffirmNewPwd.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        tvUpdateAcknowledge.setOnClickListener(pwdChange);
    }

    @Override
    public void onClick(View v) {

    }

    private View.OnClickListener pwdChange = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (cetInputNewPassword.getText().length() < 6) {
                AbToastUtil.showToast(ChangeLoginPasswordOneActivity.this,"请输入6-16位的密码!");
                return;
            }
            if (!cetInputAffirmNewPwd.getText().toString().equals(cetInputNewPassword.getText().toString())) {
                AbToastUtil.showToast(ChangeLoginPasswordOneActivity.this,"两次输入的密码不一致!");

                return;
            }
            getVerifyPwd();

        }
    };

    class textChange implements TextWatcher {

        //之后
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

        //之前
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }

        //改变    login按钮的背景
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            boolean Sing2 = false;
            if (cetInputNewPassword.getText().length() > 0) {
                Sing2 = true;

            } else {
                Sing2 = false;
            }
            boolean Sing3 = false;
            if (cetInputAffirmNewPwd.getText().length() > 0) {
                Sing3 = true;

            } else {
                Sing3 = false;
            }
            if (Sing2 == true && Sing3 == true) {
                tvUpdateAcknowledge.setBackgroundResource(R.drawable.btn_redclick_selector_one);
                tvUpdateAcknowledge.setEnabled(true);
            } else {
                tvUpdateAcknowledge.setBackgroundResource(R.drawable.btn_gray_background);
                tvUpdateAcknowledge.setEnabled(false);

            }

        }

    }

    private  void getVerifyPwd(){
        HttpParams params = new HttpParams();
//        newPassword	String	必填			新的登录密码
//        oldPassword	String	必填
        params.put("newPassword", DesUtil.md5(cetInputNewPassword.getText().toString().trim()));
//        OkHttpUtils.post(Config.RESETPASSWORD_URL).tag(this).params(params).execute(new HttpCallBack(ChangeLoginPasswordOneActivity.this) {
//            @Override
//            public void onSucceed(JSONObject result, String code, String message) {
//                if (code.equals("0")){
//                    finish();
//                    AbToastUtil.showToast(ChangeLoginPasswordOneActivity.this, message);
//                }
//
//            }
//
//            @Override
//            public void onError(String code, String message) {
//                AbToastUtil.showToast(ChangeLoginPasswordOneActivity.this, message);
//            }
//        });


        HttpUtils.post(Config.RESETPASSWORD_URL, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                finish();
                AbToastUtil.showToast(ChangeLoginPasswordOneActivity.this, message);
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(ChangeLoginPasswordOneActivity.this, message);
            }
        });
    }
}
